package cn.edu.jxnu.sse.servlet;

import cn.edu.jxnu.sse.dao.UserDao;
import cn.edu.jxnu.sse.entity.User;
import cn.edu.jxnu.sse.utils.Encrypt;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet" ,value="/login" )
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strUsername = request.getParameter("username");
        String strPwd = request.getParameter("password");
        strPwd = Encrypt.digest(strPwd);
        UserDao userDao= new UserDao();
        User user = userDao.findUserByName(strUsername);
        String target = "/index.html";
        if(user!=null&&strPwd.equalsIgnoreCase(user.getPassword())){
            target = "/WEB-INF/main.html";
        }
        RequestDispatcher disp = request.getRequestDispatcher(target);
        disp.forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
